import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    massage: "",
    btnCheckName: "全选",
    detailIndex: -1,
    aTodos: [],
    currScrollerPosition: 0,
    indexCurrScrollerPosition: 0,
    userNo: '',
    userRole: 0,
    bigTen: [],
    keepAlive: [],
    theme: 0,
  },
  getters: {
    keepAlive: state => state.keepAlive,
    GET_BIGTEN(state) {
      let bigTen = state.bigTen;
      if (bigTen.length === 0) {
        bigTen = JSON.parse(window.localStorage.getItem('bigTen')) || [];
      }
      return bigTen;
    },
    GET_USERROLE(state) {
      let userRole = window.localStorage.getItem('userRole');
      return userRole;
    },
    GET_THEME(state) {
      state.theme =parseInt(window.localStorage.getItem('theme')) ;
      return state.theme;
    },
    GET_LOGININFO(state) {
      //先从state里面获取用户登录信息
      let loginUserNo = state.userNo;
      //如果 state 里面获取不到，那么从localStorage里面获取
      if (loginUserNo == '') {
        loginUserNo = window.localStorage.getItem('userno') || '';
        state.userNo = window.localStorage.getItem('userno') || '';
      }
      console.log('-----loginUserNo-----', loginUserNo);
      return loginUserNo;
      // return "018377";
      // return "123456";


    },

    // nCounts(state) {
    // 	return state.aTodos.length;
    // }
  },
  mutations: {
    TOGGLETHEME(state, themeTag) {
      window.localStorage.setItem("theme", themeTag+"");
      state.theme = themeTag;
    },
    SET_KEEP_ALIVE: (state, keepAlive) => {
      state.keepAlive = keepAlive
    },
    UPDATEBIGTEN(state, bigTen) {
      window.localStorage.setItem("bigTen", bigTen);
      state.bigTen = JSON.parse(bigTen);
    },
    UPDATEUSERROLE(state, flag) {
      window.localStorage.setItem("userRole", flag);
      state.userRole = flag;
    },
    UPDATE_USERNO(state, userNo) {
      state.userNo = userNo;
    },
    SET_CURRSCROLLERPOSITION(state, currScrollerPosition) {
      state.currScrollerPosition = currScrollerPosition;
    },
    SET_INDEX_CURRSCROLLERPOSITION(state, tab2CurrScrollerPosition) {
      state.indexCurrScrollerPosition = tab2CurrScrollerPosition;
    },

    UPDDETAILINDEX(state, str) {
      state.detailIndex = str;
    },

    UPDBTNCHECKNAME(state, str) {
      state.btnCheckName = str;
    },
    //init ONADDALLTODO
    ONADDALLTODO(state, arr) {
      state.aTodos = arr;
    },
    //新増 TodoList item
    ONADDTODO(state, item) {
      state.aTodos.push(item);
    },
    //删除 TodoList item
    ONDELTODO(state, index) {
      state.aTodos.splice(index, 1);
    },
    //设置 错误提示信息
    ONERROR(state, str) {
      state.massage = str;
    }
  },
  actions: {
    //提交 ONADDTODO
    onAddTodo(context, item) {
      if (item.value != "") {
        context.commit("ONADDTODO", item);
        context.commit("ONERROR", "");
      } else {
        context.commit("ONERROR", "添加失败")
      }
    },
    //提交 ONDELTODO
    onDelTodo({
      commit
    }, index) {
      commit("ONDELTODO", index);
    },
    //提交 ONADDALLTODO
    onAddAllTodo({
      commit
    }, index) {
      commit("ONADDALLTODO", index);
    },
    //全选、全不选
    updBtnCheckName({
      commit
    }, checked) {
      if (checked) {
        commit("UPDBTNCHECKNAME", "全不选");
      } else {
        commit("UPDBTNCHECKNAME", "全选");
      }
    },
    //提交 ONADDALLTODO
    updDetailIndex({
      commit
    }, index) {
      commit("UPDDETAILINDEX", index);
    },
    //记录scroller位置
    setCurrscrollerPosotion({
      commit
    }, index) {
      commit("SET_CURRSCROLLERPOSITION", index);
    },
    setIndexCurrscrollerPosotion({
      commit
    }, index) {
      commit("SET_INDEX_CURRSCROLLERPOSITION", index);
    },

    updUserNo({
      commit
    }, userNo) {
      commit("UPDATE_USERNO", userNo);
    },
    setUserRole({
      commit
    }, flag) {
      commit("UPDATEUSERROLE", flag);
    },
    setBigTen({
      commit
    }, bigTen) {
      commit("UPDATEBIGTEN", bigTen);
    },
    toggleTheme({
      commit
    }, themeTag) {
      commit("TOGGLETHEME", themeTag);
    },
  },
  modules: {}
});

export default store;
